package com.nursing.think;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nursing.think.adapter.SpinnerAdapter;
import com.nursing.think.adapter.TabFragmentPagerAdapter;
import com.nursing.think.fragment.BookFragment;
import com.nursing.think.fragment.QuestionBank;
import com.nursing.think.fragment.UserFragment;
import com.nursing.think.utils.DisplayUtil;
import com.nursing.think.utils.StyleSetting;
import com.nursing.think.view.CustomSpinner;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

/**
 * 主页面
 *
 * @author: liyunsheng
 * Date: 2021-11-23
 * Time: 16:41
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {


    private LinearLayout lightLin,bookLin,userLin;
    private ImageView lightImg,bookImg,userImg;
    private TextView lightText,bookText,userText;


    private List<Fragment> list;
    private ViewPager myViewPager;
    private TabFragmentPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StyleSetting.setStatusBarColor(this, Color.parseColor("#FFF5F5F5"));
        StyleSetting.setAndroidNativeLightStatusBar(this,true);


        initView();
        initData();
        onViewClicks();

        myViewPager.setOffscreenPageLimit(3);
    }

    private void onViewClicks() {
        lightLin.setOnClickListener(this);
        bookLin.setOnClickListener(this);
        userLin.setOnClickListener(this);
        myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                myViewPager.setCurrentItem(position);
                switch (position){
                    case 0:
                        clearViewStyle();
                        lightImg.setImageResource(R.mipmap.icon_light_sel);
                        lightText.setTextColor(Color.parseColor("#FF763EE7"));
                        break;
                    case 1:
                        clearViewStyle();
                        bookImg.setImageResource(R.mipmap.icon_book_sel);
                        bookText.setTextColor(Color.parseColor("#FF763EE7"));
                        break;
                    case 2:
                        clearViewStyle();
                        userImg.setImageResource(R.mipmap.icon_user_sel);
                        userText.setTextColor(Color.parseColor("#FF763EE7"));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        list.add(new QuestionBank());
        list.add(new BookFragment());
        list.add(new UserFragment());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(0);  //初始化显示第一个页面

    }

    /**
     * 控件初始化
     */
    private void initView() {
        myViewPager = findViewById(R.id.myViewPager);

        lightLin = findViewById(R.id.lightLin);
        lightImg = findViewById(R.id.lightImg);
        lightText = findViewById(R.id.lightText);
        bookLin = findViewById(R.id.bookLin);
        bookImg = findViewById(R.id.bookImg);
        bookText = findViewById(R.id.bookText);
        userLin = findViewById(R.id.userLin);
        userImg = findViewById(R.id.userImg);
        userText = findViewById(R.id.userText);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lightLin:
                clearViewStyle();
                lightImg.setImageResource(R.mipmap.icon_light_sel);
                lightText.setTextColor(Color.parseColor("#FF763EE7"));
                myViewPager.setCurrentItem(0);
                break;
            case R.id.bookLin:
                clearViewStyle();
                bookImg.setImageResource(R.mipmap.icon_book_sel);
                bookText.setTextColor(Color.parseColor("#FF763EE7"));
                myViewPager.setCurrentItem(1);
                break;
            case R.id.userLin:
                clearViewStyle();
                userImg.setImageResource(R.mipmap.icon_user_sel);
                userText.setTextColor(Color.parseColor("#FF763EE7"));
                myViewPager.setCurrentItem(2);
                break;

        }
    }

    /**
     * 清楚按钮选中样式
     */
    private void clearViewStyle(){
        lightImg.setImageResource(R.mipmap.icon_light_nor);
        lightText.setTextColor(Color.parseColor("#CC000000"));
        bookImg.setImageResource(R.mipmap.icon_book_nor);
        bookText.setTextColor(Color.parseColor("#CC000000"));
        userImg.setImageResource(R.mipmap.icon_user_nor);
        userText.setTextColor(Color.parseColor("#CC000000"));
    }
}